home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part1 / 9652 < prev    next >
Encoding:
Text File  |  1996-08-05  |  9.1 KB  |  446 lines

  1. Path: lantana.singnet.com.sg!usenet
  2. From: Teddy Bear <s7700038@singnet.com.sg>
  3. Newsgroups: comp.lang.c
  4. Subject: Something wrong with my count
  5. Date: 12 Mar 1996 16:20:29 GMT
  6. Organization: Singapore Telecom Internet Service
  7. Message-ID: <4i488d$7ip@lantana.singnet.com.sg>
  8. NNTP-Posting-Host: ts900-1122.singnet.com.sg
  9. Mime-Version: 1.0
  10. Content-Type: multipart/mixed;
  11.     boundary="-------------------------------127682262510553"
  12. X-Mailer: Mozilla 1.22 (Windows; I; 16bit)
  13.  
  14. This is a multi-part message in MIME format.
  15.  
  16. ---------------------------------127682262510553
  17. Content-Transfer-Encoding: 7bit
  18. Content-Type: text/plain; charset=us-ascii
  19.  
  20. I still can't solve my problem
  21. Can any kind soul help me?
  22. Basically i can't save 2 or more particulars into the list
  23. They'll give me rubbish when i load back from disk
  24. Something is wron with my count, i think
  25. Please help me ASAP
  26. Only a few more hours left
  27.  
  28.  
  29. ---------------------------------127682262510553
  30. Content-Transfer-Encoding: 7bit
  31. Content-Type: text/plain
  32.  
  33. #include <stdio.h>
  34. #include <conio.h>
  35. #include <string.h>
  36. #include <stdlib.h>
  37.  
  38.  
  39. typedef struct node{
  40.     int age;               //individual's age
  41.     char name[40];         //individual's name
  42.     char address[40];     //individual's address
  43.     struct node *next;
  44. }node;
  45. node *head;
  46. typedef node* nodeptr;
  47.  
  48. void createlist(nodeptr *ptrtohead, nodeptr head);
  49. int  countlist(nodeptr head);
  50. nodeptr searchlist(nodeptr head, char *wanted);
  51. void editlist(nodeptr head);
  52. void displaylist(nodeptr head);
  53. void save_record(FILE *fileptr, nodeptr *head, int count);
  54. void load_record(FILE *fileptr, nodeptr *head, nodeptr *tail, int *count);
  55. nodeptr addlist(nodeptr head);
  56. nodeptr deletelist(nodeptr head);
  57. int main()
  58. {
  59.     char option;
  60.     nodeptr ptr, *ptrtohead, head, tail;
  61.     int count=0;
  62.     FILE *fileptr;
  63.     head = NULL;
  64.  
  65.     option = '1';
  66.     do
  67.     {
  68.     clrscr();
  69.     printf("\n\n\nThis program .....");
  70.     printf("\n\n1) Read existing list from disk\n");
  71.     printf("2) Add list\n");
  72.     printf("3) Delete list\n");
  73.     printf("4) Search list\n");
  74.     printf("5) Display List\n");
  75.     printf("6) Save list to disk\n");
  76.     printf("7) Kill Yi Yong !!! ( EXIT )\n");
  77.     printf("\nPlease enter your choice : ");
  78.  
  79.     if(option < '1' || option > '7')
  80.         printf("\nInvaild option. Please re-enter:");
  81.     option = getche();
  82.     switch(option)
  83.     {
  84.         case '1' : load_record(fileptr, &head, &tail, &count);
  85.                displaylist(head);
  86.                printf("\n\nPress any key to continue.. ");
  87.                getch();
  88.                break;
  89.         case '2' : if(head == 0)
  90.                {
  91.                createlist(ptrtohead, NULL);
  92.                head = *ptrtohead;
  93.                }
  94.                else
  95.                head = addlist(head);
  96.                printf("\n\nPress any key to continue.. ");
  97.                getch();
  98.                break;
  99.         case '3' : if (head == 0)
  100.             printf("nothing to delete");
  101.             head = deletelist(head);
  102.                printf("\n\nPress any key to continue.. ");
  103.                getch();
  104.                break;
  105.         case '4' : editlist(head);
  106.                printf("\n\nPress any key to continue.. ");
  107.                getch();
  108.                break;
  109.         case '5' : displaylist(head);
  110.                printf("\nPress any key to cont...");
  111.                getch();
  112.                break;
  113.         case '6' :   save_record(fileptr, &head, count);
  114.                displaylist(head);
  115.                break;
  116.         case '7' : clrscr();
  117.                exit(0);
  118.  
  119.     }
  120.     }
  121.     while(option != '7');
  122.     return 0;
  123. }
  124.  
  125. void load_record(FILE *fileptr, nodeptr *head, nodeptr *tail, int *count)
  126. //get list.dat from disk
  127. {
  128.      nodeptr load;
  129.      int node_size=sizeof(node);
  130.      int name_length,address_length;
  131.  
  132.      if((fileptr=fopen("b:list.dat","rb"))==NULL)
  133.      {
  134.       printf("\a\n\nUnable to open file\n");
  135.       printf("\nPress any key to continue....");
  136.       getch();
  137.      }
  138.      else
  139.      {
  140.       fscanf(fileptr,"%d\n", &(*count));
  141.       while(!feof(fileptr))
  142.       {
  143.           load=(nodeptr)malloc(node_size);
  144.           fscanf(fileptr, "%d\n", &name_length);
  145.           fscanf(fileptr, "%d\n", &address_length);
  146.           fgets(load->name,name_length+1, fileptr);
  147.           fscanf(fileptr,"\n");
  148.           fgets(load->address, address_length+1, fileptr);
  149.           fscanf(fileptr, "%d\n",  &load->age);
  150.  
  151.           if((*head)==NULL)
  152.           {
  153.            (*head)=load;
  154.            (*head)=load;
  155.           }
  156.           else
  157.           {
  158.            (*tail)->next=load;
  159.            (*tail)=load;
  160.           }
  161.       }
  162.       (*tail)->next=NULL;
  163.      }
  164.      fclose(fileptr);
  165.      return;
  166. }
  167.  
  168.  
  169. void save_record(FILE *fileptr, nodeptr *head, int count)
  170. //save info to disk file list.dat
  171. {
  172.      nodeptr save;
  173.  
  174.      if ((fileptr=fopen("b:list.dat","wb"))==NULL)
  175.      {
  176.       printf("\a\n\nERROR!!!!Unable to open file!!!\n");
  177.      }
  178.      else
  179.      {
  180.       save=(*head);
  181.       fprintf(fileptr,"%d\n",count);
  182.       while(save!=NULL)
  183.       {
  184.            fprintf( fileptr,"%d\n",  strlen(save->name));
  185.            fprintf( fileptr, "%d\n", strlen(save->address));
  186.            fprintf( fileptr, "%s\n", save->name);
  187.            fprintf( fileptr, "%s\n", save->address);
  188.            fprintf( fileptr, "%d\n", save->age);
  189.            save=save->next;
  190.       }
  191.      }
  192.      fclose(fileptr);
  193.      printf("\a\n\nSaving Done!!!!!\n");
  194.      printf("\nPress any key to continue.....");
  195.      return;
  196. }
  197.  
  198.  
  199.  
  200. void createlist(nodeptr *ptrtohead, nodeptr head)
  201. //create a link list
  202. {
  203.     nodeptr temp, last;
  204.     char ans;
  205.     int size = sizeof(node);
  206.     do
  207.     {
  208.     temp=(nodeptr)malloc(size);
  209.     clrscr();
  210.     printf("\nEnter Name    : ");
  211.     gets(temp->name);
  212.     printf("Enter Age     : ");
  213.     scanf("%d",&temp->age);
  214.     fflush(stdin);
  215.     printf("Enter Address : ");
  216.     gets(temp->address);
  217.  
  218.     if(head == NULL)
  219.     {
  220.         head = temp;
  221.         last = temp;
  222.     }
  223.     else
  224.     {
  225.         last->next=temp;
  226.         last = temp;
  227.     }
  228.     printf("\nWould u like to enter more ? <Y/N> : ");
  229.     ans=toupper(getch());
  230.     }
  231.     while(ans != 'N');
  232.     last->next = NULL;
  233.     *ptrtohead = head;
  234. }
  235.  
  236. nodeptr searchlist(nodeptr head, char *want)
  237.  
  238. {
  239.     int element, count, found, condition;
  240.     nodeptr ptr;
  241.     element = countlist(head);
  242.     ptr = head;
  243.     found = 0;
  244.     condition = 1;
  245.     for(count=0; (count<element)&&(condition); )
  246.     {
  247.     if((strcmp(ptr->name, want))==0)
  248.     {
  249.         found = 1;
  250.         condition = 0;
  251.     }
  252.     else
  253.         if((strcmp(ptr->name, want)) > 0)
  254.         condition = 0;
  255.         else
  256.         {
  257.         ptr = ptr->next;
  258.         count++;
  259.         }
  260.     }
  261.  
  262.     if(found)
  263.     return ptr;
  264.     else
  265.     return NULL;
  266. }
  267.  
  268. int countlist(nodeptr head)
  269. //count no of individuals
  270. {
  271.     int count;
  272.     nodeptr ptr;
  273.     ptr = head;
  274.     if(ptr != NULL)
  275.     {
  276.     for(count=1;ptr->next!=NULL; count++)
  277.     ptr = ptr->next;
  278.     return count;
  279.     }
  280.     else
  281.     {
  282.     return 0;
  283.     }
  284. }
  285.  
  286. void  editlist(nodeptr head)
  287. {
  288.  
  289.     char *find, answer='Y';
  290.     int cnt, check;
  291.     nodeptr ptrtoelm;
  292.     do
  293.     {
  294.     clrscr();
  295.     printf("\n\nEnter Name   :  ");
  296.     gets(find);
  297.     ptrtoelm = searchlist(head, find);
  298.     if(ptrtoelm==NULL)
  299.     {
  300.         printf("\a\nSORRY! THERE IS NO SUCH NAME\n");
  301.         printf("\nDo you wish to continue searching? <Y/N> : ");
  302.         answer = toupper(getch());
  303.     }
  304.     if(answer == 'N')
  305.         return;
  306.    }while(ptrtoelm==NULL && answer == 'Y');
  307.  
  308.  
  309.     printf("\nName    : %s\n", ptrtoelm->name);
  310.     printf("Age     : %d\n", ptrtoelm->age);
  311.     printf("Address : %s\n", ptrtoelm->address);
  312. }
  313.  
  314.  
  315. void displaylist(nodeptr head)
  316. // Display the information in the link.
  317. {
  318.     nodeptr ptr;
  319.     ptr = head;
  320.     clrscr();
  321.     while(ptr!=NULL)
  322.     {
  323.     printf("\nNAME: %s \n",ptr->name);
  324.     printf("AGE: %d \n",ptr->age);
  325.     printf("ADDRESS: %s \n",ptr->address);
  326.     ptr = ptr->next;
  327.     }
  328.  
  329. }
  330.  
  331. nodeptr clearlist(nodeptr head)
  332.  
  333. {
  334.     nodeptr ptr;
  335.     while(head != NULL)
  336.     {
  337.     ptr = head;
  338.     head = head->next;
  339.     free(ptr);
  340.     }
  341.     free(ptr);
  342.     return head;
  343. }
  344.  
  345. nodeptr addlist(nodeptr head)
  346.  
  347. {
  348.  
  349.     nodeptr front, back, newitem;
  350.     int check, cnt;
  351.     newitem = (nodeptr)malloc(sizeof(node));
  352.     clrscr();
  353.     printf("\n\nEnter name to be added : ");
  354.     gets(newitem->name);
  355.     if((strcmp((back->name),(front->name))) == 0)
  356.     {
  357.     printf("\a");
  358.     printf("\n\nDUPLICATE COPY");
  359.     strcpy(back->name,front->name);
  360.     back->age = front->age;
  361.     strcpy(back->address,front->address);
  362.     getch();
  363.     }
  364.     fflush(stdin);
  365.     printf("Enter age              : ");
  366.     scanf("%d", &newitem->age);
  367.     fflush(stdin);
  368.     printf("Enter address          : ");
  369.     gets(newitem->address);
  370.     if((strcmp(newitem->name, head->name)) <= 0)
  371.     {
  372.     newitem->next = head;
  373.     head = newitem;
  374.     }
  375.     else
  376.     {
  377.     back = head;
  378.     front = back->next;
  379.     while(front!=NULL)
  380.     {
  381.         if((strcmp(newitem->name, front->name)) > 0)
  382.         {
  383.         back = front;
  384.         front = front->next;
  385.         }
  386.         else
  387.         break;
  388.     }
  389.     newitem->next = front;
  390.     back->next = newitem;
  391.     }
  392.     return head;
  393. }
  394.  
  395. nodeptr deletelist(nodeptr head)
  396.  
  397. {
  398.     nodeptr front, back;
  399.     char *tempname;
  400.     if(head != NULL)
  401.     {
  402.     back = head;
  403.     front = back->next;
  404.     }
  405.     else
  406.     {
  407.     printf("\n\aDelete not allowed.");
  408.     getch();
  409.     return NULL;
  410.     }
  411.     tempname = (char *)malloc(sizeof(char *));
  412.     clrscr();
  413.     printf("\n\nEnter name to be delete : ");
  414.     gets(tempname);
  415.     if((strcmp(tempname,back->name)) == 0)
  416.     {
  417.     front = head;
  418.     back = back->next;
  419.     free(front);
  420.     return back;
  421.     }
  422.     else
  423.     {
  424.     while(((strcmp(tempname, front->name))!=0) && (front!=NULL))
  425.     {
  426.         back = front;
  427.         front = front->next;
  428.     }
  429.     if(front==NULL)
  430.     {
  431.         printf("\n\aNo such person in the list.");
  432.         return head;
  433.     }
  434.     else
  435.     {
  436.         back->next = front->next;
  437.         free(front);
  438.         return head;
  439.     }
  440.     }
  441.  
  442. }
  443.  
  444.  
  445. ---------------------------------127682262510553--
  446.